<template>
  <a-modal
    :width="500"
    :visible="visible"
    title="设置团队"
    @cancel="onClose"
    ok-text="确定"
    :destroyOnClose="true"
  >
    <a-alert type="warning" style="margin-bottom: 20px">
      <template #description>
        <p>更改团队可能会影响该团队成员的未结算提成金额，请谨慎操作。</p>
      </template>
    </a-alert>
    <a-spin :spinning="spinning">
      <a-form>
        <a-row :wrap="true" :gutter="12">
          <a-col :span="24">
            <a-form-item label="人员信息">
              <span>{{personnelInfo.name || '--'}}/{{personnelInfo.tel || '--'}}</span>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="选择团队" v-bind="validateInfos.team_id">
              <a-select
                v-model:value="formData.team_id"
                :options="teamOptions"
                placeholder="请选择招商团队"
                :allowClear="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>


    <template #footer>
      <a-row type="flex" align="middle" justify="center">
        <a-col :flex="1" style="text-align: left">
          <a-checkbox v-model:checked="isAgree">勾选，代表确定知晓操作带来的影响。</a-checkbox>
        </a-col>
        <a-col>
          <a-button @click="onClose">取消</a-button>
          <a-button
            :disabled="!isAgree"
            type="primary"
            :loading="saveLoading"
            @click="onSubmit"
          >确定</a-button
          >
        </a-col>
      </a-row>
    </template>
  </a-modal>

</template>
<script>
import { defineComponent, reactive, toRefs, computed, watch } from "vue";
import { Form, message } from "ant-design-vue";
import store from "../../../../store";
import request from "../../../../common/utils/request";
const useForm = Form.useForm;
export default defineComponent({
  name: "SetBusinessTeam",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    personnelInfo: {
      type: Object,
      default: () => ({}),
    }
  },
  components: {},
  setup(props, { emit }) {
    const state = reactive({
      saveLoading: false,
      isAgree: false,
      teamOptions: [],
      spinning: false,
    });

    const formData = reactive({
      team_id: undefined,
    });
    const ruleRef = reactive({
      team_id: [{ required: true, message: "必选" }],
    });

    const { resetFields, validate, validateInfos} = useForm(formData, ruleRef);

    const onSubmit = () => {
      validate().then(() => {
        let params = {
          id: props.personnelInfo?.id,
        };
        for (let key in formData) {
          if (formData[key] || formData[key] === 0) {
            params[key] = formData[key];
          }
        }
        state.saveLoading = true;
        request.put("/org", "/business/customerTeam/updateUserTeam", params).then((res) => {
          message.success(res.msg || "设置成功!");
          emit("success");
        }).finally(() => {
          state.saveLoading = false;
        });
      });
    };




    const onClose = () => {
      emit("handleCancel");
    };

    const getTeamList = () => {
      return new Promise((resolve) => {
        state.spinning = true;
        request.get("/org", "/business/customerTeam/teamIndex", {}).then((res) => {
          let teamList = res?.data || [];
          state.teamOptions = teamList.map((v) => {
            return {
              label: v.name,
              value: v.id,
            }
          })
          resolve();
        }).finally(() => {
          state.spinning = false;
        });
      });
    };


    const init = () => {
      getTeamList().then(() => {
        formData.team_id = props.personnelInfo?.team_id || undefined;
      })
    };

    const reset = () => {
      state.saveLoading = false;
      state.isAgree = false;
      state.teamOptions = [];
      state.spinning = false;
    };

    watch(
      () => props.visible,
      (val) => {
        if (val) {
          init();
        } else {
          resetFields();
          reset();
        }
      }
    );

    return {
      ...toRefs(state),
      formData,
      validateInfos,
      onClose,
      onSubmit,
      getTeamList,
    };
  },
});
</script>

<style lang="less" scoped>
p {
  &:last-of-type {
    margin-bottom: 0;
  }
}
.user-tag :deep(.ant-tag) {
  border-radius: 20px !important;
  padding: 0 12px 0 0 !important;
  background: #f1f1f1;
  border: none;
}
:deep(.ant-alert-description) {
  font-size: 12px;
  p {
    margin-bottom: 6px;
    &:last-of-type {
      margin-bottom: 0;
    }
  }
}
:deep(.ant-alert-with-description.ant-alert-no-icon) {
  padding: 6px 10px;
}
</style>
